<?php
namespace Admin\Model;

class FundModel extends \Think\Model
{
	//自动验证
	protected $_validate = array(
		array('id', '/^\d+$/', '参数错误', 1, 'regex', 2),
//		array('fund_code', '/^\w+$/', '请输入正确的基金代码', 1),
//		array('fund_code', '', '基金代码已存在', 1, 'unique'),
//		array('fund_abb', 'require', '请输入基金简称', 1),
		array('fund_name', 'require', '请输入正确的基金名称', 1),
//		array('section_code', '/^\d$/', '请选择区域代码', 1),
		array('fund_type', '/^[12]{2}$/', '请选择基金类型', 1),
		array('contract_id', '/^[1-9]\d*$/', '请选择基金合同', 1),
		array('contract_name', '/^[\w\x{4e00}-\x{9fa5}]{2,}$/u', '请选择基金合同', 1),
//		array('trader_id', '/^[1-9]\d*$/', '请选择基金经理', 1),
//		array('trader_name', '/^[\w,\x{4e00}-\x{9fa5}]{2,}$/u', '请选择基金经理1', 1),
//		array('cat_ids', '/^[1-9][\d,]*$/', '请选择基金分类', 1),
//		array('cat_names', '/^[\w,\x{4e00}-\x{9fa5}]{2,}$/u', '请选择基金分类1', 1),
//		array('fund_trusteeship', '/^[\w\x{4e00}-\x{9fa5}]{2,}$/u', '请输入基金托管人', 1),
//		array('trusteeship_rate', '/^(\d{1,2})|(\d{1,2}\.\d{0,2})$/', '请输入正确的基金托管费率', 1),
//		array('fund_admin', '/^[\w\x{4e00}-\x{9fa5}]{2,}$/u', '请输入正确的基金管理人', 1),
//		array('admin_rate', '/^(\d{1,2})|(\d{1,2}\.\d{0,2})$/', '请输入正确的基金管理费率', 1),
		array('expected_rate', '/^(\d{1,4})|(\d{1,2}\.\d{0,2})$/', '请输入正确的基金预期收益率', 1),
		array('expected_rate_desc', 'require', '请输入预期收益率说明', 1),
//		array('estb_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入证券投资基金成立时间称', 0),
//		array('list_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入证券投资基金上市时间', 2),
		array('open_start_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入开放期开始日期', 1),
		array('open_back_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入开放期赎回起始日', 1),
		array('open_end_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入开放期结束日期', 1),
		array('trade_time', '/^2\d{3}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/', '请输入开始购买日期', 1),
		array('open_desc', 'require', '请输入开放日说明', 1),
		array('close_desc', 'require', '请输入封闭期说明', 1),
		array('total_count', '/^\d+$/', '请输基金总份数', 1),
		//array('min_amount', '/^\d*|\d*\.\d*$/', '请输入最小购买金额', 1),
		array('min_count', '/^\d+$/', '请输入最小购买份数', 1),
		//array('max_amount', '/^\d*|\d*\.\d*$/', '请输入最大购买金额', 1),
		array('max_count', '/^\d+$/', '请输入最大购买份数', 1),
		array('sort_order', '/^\d+$/', '请输入序号', 1),
//		array('fund_brief', '/^[\w，；“”‘’《》。、！,;\'".<>\x{4e00}-\x{9fa5}]{2,}$/u', '请输入正确的基金简称', 1),
		array('is_recommend', 'checkRecommend', '请输入推荐理由', 1, 'callback'),
			
	);
	
	//自动完成
	protected $_auto = array(
		array('create_time', 'time', 1, 'function'),
		array('update_time', 'time', 3, 'function'),
		array('status', 1, 1, 'string')
	);
	
	/**
	 * 添加基金产品
	 * @author 刘新
	 */
	public function addFund()
	{
		//创建数据对象
		if($this->create() === false) return array('error' => '1', 'msg' => $this->getError());
		if(I('estb_time')) $this->estb_time = strtotime(I('estb_time'));
		if(I('list_time')) $this->list_time = strtotime(I('list_time'));
		if(I('open_start_time')) $this->open_start_time = strtotime(I('open_start_time'));
		if(I('open_back_time')) $this->open_back_time = strtotime(I('open_back_time'));
		if(I('open_end_time')) $this->open_end_time = strtotime(I('open_end_time'));
		if(I('trade_time')) $this->trade_time = strtotime(I('trade_time'));
		
		//开启事务
		$this->startTrans();
		$return = array('error' => '0', 'msg' => '添加成功');
		
		try {
			//添加基金产品
			$res = $this->add();
			
			if(!$res) {
				throw new \Think\Exception('添加失败');
			}
			
			$catIds = explode(',', I('cat_ids'));
			$catNames = explode(',', I('cat_names'));
			
			$data = array();
			$now = time();
			
			foreach($catIds as $key => $catId) {
				$tmp['fund_id'] = $res;
				$tmp['fund_name'] = I('fund_name');
				$tmp['cat_id'] = $catId;
				$tmp['cat_name'] = $catNames[$key];
				$tmp['create_time'] = $now;
				$tmp['update_time'] = $now;
				$tmp['status'] = 1;
				$data[] = $tmp;
			}
			
			//添加基金分类关系
			$res = M('FundCategories')->addAll($data);
			
			if(!$res) {
				throw new \Think\Exception('基金分类关系更新失败');
			}
			
			//更新基金经理表信息
			$res = M('Trader')->where(array('id' => I('trader_id')))->setInc('count', 1);
			if($res === false) {
				throw new \Think\Exception('基金经理信息更新失败'. I('trader_id'));
			}

			$this->commit();
		} catch (\Think\Exception $e) {
			$this->rollback();
			$return['error'] = 1;
			$return['msg'] = $e->getMessage();
		}
		
		return $return;
	}
	
	/**
	 * 编辑基金产品
	 * @author 刘新
	 */
	public function editFund()
	{
		//创建数据对象
		if($this->create() === false) return array('error' => '1', 'msg' => $this->getError());
		if(I('estb_time')) $this->estb_time = strtotime(I('estb_time'));
		if(I('list_time')) $this->list_time = strtotime(I('list_time'));
		if(I('open_start_time')) $this->open_start_time = strtotime(I('open_start_time'));
		if(I('open_back_time')) $this->open_back_time = strtotime(I('open_back_time'));
		if(I('open_end_time')) $this->open_end_time = strtotime(I('open_end_time'));
		if(I('trade_time')) $this->trade_time = strtotime(I('trade_time'));
		
		//开启事务
		$this->startTrans();
		$return = array('error' => '0', 'msg' => '编辑成功');
		
		try {
			//保存更新
			$res = $this->save();
			
			if(!$res) {
				throw new \Think\Exception($this->getError());
			}
			
			//获取基金所属分类信息
			$catIds = explode(',', I('cat_ids'));
			$catNames = explode(',', I('cat_names'));
			$editId = I('id');
			$data = array();
			$now = time();
				
			foreach($catIds as $key => $catId) {
				$tmp['fund_id'] = $editId;
				$tmp['fund_name'] = I('fund_name');
				$tmp['cat_id'] = $catId;
				$tmp['cat_name'] = $catNames[$key];
				$tmp['create_time'] = $now;
				$tmp['update_time'] = $now;
				$tmp['status'] = 1;
				$data[] = $tmp;
			}
			
			//先删除原来的基金分类关系
			$res = M('FundCategories')->where(array('fund_id' => I('id'), 'status' => 1))->delete();
			if($res === false) throw new \Think\Exception('分类信息保存失败2');
			
			//保存基金分类关系
			$res = M('FundCategories')->addAll($data);
			if(!$res) {
				throw new \Think\Exception('分类信息保存失败1');
			}
			
			//更新基金净值表中的fund_name
			$res = M('FundNetvalue')->where(array('fund_id' => $editId, 'status' => 1))->save(array('fund_name' => I('fund_name')));
			if($res === false) throw new \Think\Exception('基金净值更新失败');
			$this->commit();
		} catch (\Think\Exception $e) {
			$this->rollback();
			$return['error'] = 1;
			$return['msg'] = $e->getMessage();
		}
	
		return $return;
	}
	
	/**
	 * 推荐校验
	 * @param int $value
	 * @author 刘新
	 */
	protected function checkRecommend($value)
	{
		//1不推荐，不用校验推荐理由
		if($value == 1) {
			return true;
		//2推荐，需要校验推荐理由
		} else if($value == 2) {
			$reg = '/^[\w，；“”‘’《》。、！,;\'".<>\x{4e00}-\x{9fa5}]{2,}$/u';
			$res = preg_match($reg, I('recommend_note'));
			
			return (boolean)$res;
		}
	}
}